package com.cgc.shop.action;

import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cgc.shop.model.Product;
import com.cgc.shop.service.AccountService;
import com.cgc.shop.service.CartService;
import com.cgc.shop.service.CategoryService;
import com.cgc.shop.service.OrdersService;
import com.cgc.shop.service.ProductService;
import com.cgc.shop.service.StatusService;
import com.cgc.shop.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller("baseAction")
@Scope("prototype")
public class BaseAction<T> extends ActionSupport implements RequestAware, SessionAware, ApplicationAware, ModelDriven<T> {
	
	/**
	 * 前台传来要删除的id序列
	 */
	protected String ids;
	
	/**
	 * 用于返回给前台的stream
	 */
	protected InputStream inputStream;
	
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
	/**
	 * 由EasyUI传进来，代表页码
	 */
	protected Integer page;
	/**
	 * 由EasyUI传进来，代表行数
	 */
	protected Integer rows;
	/**
	 * 用于存放要转化成json数据的对象，然后传给EasyUI显示，此成员要在struts.xml中配置
	 */
	protected Map<String, Object> jsonMap;
	
	/**
	 * 存放泛型T的列表，用于转化成json数据
	 */
	protected List<T> jsonList;
	public List<T> getJsonList() {
		return jsonList;
	}

	public void setJsonList(List<T> jsonList) {
		this.jsonList = jsonList;
	}
	@Resource
	protected AccountService accountService;
	
	@Resource
	protected CartService cartService;
	
	@Resource
	protected OrdersService ordersService;
	
	@Resource
	protected ProductService productService;
	
	@Resource
	protected StatusService statusService;
	
	@Resource
	protected UserService userService;
	
	@Resource
	protected CategoryService categoryService;
	
	protected Map<String, Object> applicationMap;
	protected Map<String, Object> sessionMap;
	protected Map<String, Object> requestMap;
	
	protected T model;
	
	@Override
	public void setApplication(Map<String, Object> applicationMap) {
		this.applicationMap = applicationMap;
	}

	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
		
	}

	@Override
	public void setRequest(Map<String, Object> requestMap) {
		this.requestMap = requestMap;
		
	}

	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getRows() {
		return rows;
	}
	public void setRows(Integer rows) {
		this.rows = rows;
	}
	
	public Map<String, Object> getJsonMap() {
		return jsonMap;
	}
	@Override
	public T getModel() {
		ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();  
        Class clazz = (Class)type.getActualTypeArguments()[0];
        try {
			model = (T) clazz.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
        return model;
	}

}
